package com.api.shopapi.mapper;


import com.api.shopapi.entity.OrderGoods;
import com.api.shopapi.entity.dto.OrderGoodsDTO;
import com.api.shopapi.entity.dto.OrderGoodsDTO1;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface OrderGoodsMapper {

    @Insert("insert into order_goods" +
            "(" +
            "OrderGoodsCode,"+
            "OrderBusinessCode,"+
            "GoodsCode,"+
            "GoodsName,"+
            "GoodsPrice,"+
            "GoodsImg,"+
            "Number,"+
            "PlanTime"+
            ")" +
            "values" +
            "(" +
            "#{orderGoods.OrderGoodsCode}," +
            "#{orderGoods.OrderBusinessCode}," +
            "#{orderGoods.GoodsCode}," +
            "#{orderGoods.GoodsName}," +
            "#{orderGoods.GoodsPrice}," +
            "#{orderGoods.GoodsImg}," +
            "#{orderGoods.Number}," +
            "#{orderGoods.PlanTime}" +
            ")")
    public int insert(@Param("orderGoods") OrderGoods orderGoods);

    @Delete("delete from order_goods where OrderGoodsCode=#{orderGoodsCode}")
    public int delete(@Param("orderGoodsCode") String orderGoodsCode);

    @Update("update order_goods set " +
            "OrderBusinessCode = #{orderGoods.OrderBusinessCode},"+
            "GoodsCode = #{orderGoods.GoodsCode}," +
            "GoodsName = #{orderGoods.GoodsName}," +
            "GoodsPrice = #{orderGoods.GoodsPrice}," +
            "GoodsImg = #{orderGoods.GoodsImg},"+
            "Number = #{orderGoods.Number}," +
            "PlanTime = #{orderGoods.PlanTime}" +
            " where OrderGoodsCode=#{orderGoods.OrderGoodsCode}")
    public int update(@Param("orderGoods") OrderGoods orderGoods);

    @Select("select * from order_goods")
    public List<OrderGoods> getList();

    @Select("select * from order_goods where OrderGoodsCode=#{orderGoodsCode} limit 1")
    public OrderGoods getByOrderGoodsCode(@Param("orderGoodsCode") String orderGoodsCode);

    @Select("select * from order_goods where orderBusinessCode=#{orderBusinessCode} ")
    public List<OrderGoodsDTO> getListByOrderBusinessCode(@Param("orderBusinessCode") String orderBusinessCode);


    @Select("select  * from order_goods where OrderBusinessCode=#{orderBusinessCode} limit 1")
    OrderGoods getByOrderBusinessCode(@Param("orderBusinessCode") String orderBusinessCode);

    @Select("select * from order_goods as og " +
            "left join order_business ob on og.OrderBusinessCode = ob.OrderBusinessCode     " +
            "where og.OrderBusinessCode=#{orderBusinessCode} and ob.BusinessCode=#{businessCode} and ob.OrderType=1 ")
    List<OrderGoods> getByOrderBusinessCodeForPage(@Param("orderBusinessCode") String orderBusinessCode,
                                                   @Param("businessCode") String businessCode);


    @Select("<script>" +
            "select * from order_goods og left join order_business ob on og.OrderBusinessCode = ob.OrderBusinessCode " +
            "where ob.BusinessCode =#{businessCode} " +
            "<if test =' orderType !=-1 '>" +
            "and ob.OrderType=#{orderType} " +
            "</if>" +
            "and ob.Status in(3,7,14,16) " +
            "and ob.CreateTime between #{startTime} and #{endTime} " +
            "order by ob.CreateTime desc " +
            "</script>")
    List<OrderGoodsDTO1>  businessConsumerOrderInfo(@Param("startTime")String startTime,@Param("endTime")String endTime,
                                                    @Param("orderType")int orderType,@Param("businessCode")String businessCode);





    @Select("<script>" +
            "select * from order_goods og left join order_business ob on og.OrderBusinessCode = ob.OrderBusinessCode " +
            "left join `order` o on ob.OrderCode=o.OrderCode " +
            "where o.UserCode=#{userCode} " +
            "<if test =' businessCode !=\"\" and businessCode!=null '>" +
            " and ob.BusinessCode =#{businessCode} " +
            "</if>" +
            "<if test =' orderType !=-1 '>" +
            "and ob.OrderType=#{orderType} " +
            "</if>" +
            "and ob.Status in(3,7,14,16) " +
            "and ob.CreateTime between #{startTime} and #{endTime} " +
            "order by ob.CreateTime desc " +
            "</script>")
    List<OrderGoodsDTO1> userConsumerOrderInfo(@Param("userCode")String userCode,@Param("startTime")String startTime,@Param("endTime")String endTime,
                                               @Param("orderType")int orderType,@Param("businessCode")String businessCode);
}
